package com.sky.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.sky.WebSocket.WebSocketServer;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController("用户订单接口")
@RequestMapping("/user/order")
@Api(tags = "用户下单接口")
public class orderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WebSocketServer webSocketServer;
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> OrderSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
       OrderSubmitVO orderSubmitVO =  orderService.OrdeSubmit(ordersSubmitDTO);
       return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        //跳过微信支付，提交订单就设置订单状态为支付
        String orderNumber = ordersPaymentDTO.getOrderNumber();
        orderService.paySuccess(orderNumber);
        return Result.success(orderPaymentVO);

    }

    /**
     * 查询历史订单
     */

    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> selectHistoryOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
            log.info("历史订单查询{}", ordersPageQueryDTO);
            PageResult pageResult = orderService.selectHistoryOrders(ordersPageQueryDTO);
            return Result.success(pageResult);
    }

    /**
     * 历史订单详情
     */

    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetailed(@PathVariable Long id){
       log.info("历史订单详情查询");
       OrderVO orderVO = orderService.selcetOrderDetail(id);
       return Result.success(orderVO);
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{id}")
    public Result cencelOrder(@PathVariable Long id){
        log.info("用户取消订单，取消的订单id为：{}",id);
        orderService.cencel(id);
        return Result.success();
    }

    /**
     * 再来一单
     */
    @PostMapping("/repetition/{id}")
    public Result Repetition(@PathVariable Long id){
        log.info("再来一单，再来一单的id为：{}",id);
        orderService.repetition(id);
        return Result.success();
    }
    /**
     * 催单
     */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable("id") Integer id){
        log.info("用户催单");
        orderService.reminder(id);
        return Result.success();
    }
}


